# a script that compiles the results of Monte Carlo runs stored in structure arrays in multiple batches (necessary to calculate the baseline estimates), reads the alternative estimates based on the simplified, bi-proportional allocation of market shares and re-arranges the data in structure arrays that can be loaded by other scripts to draw charts
# version: Dec 2020
# ENSURE that Octave can find the script, use the "addpath" command: addpath ("/[insert the path to your Octave folder]/Octave")

# enter year 0 (2005 <= y0 <= 2014) and year 1 (2006 <= y1 <= 2015)
year = input ("enter year 0: ")
y0 = num2str (year);
year = input ("enter year 1: ")
y1 = num2str (year);
# define file name to load the results of Monte Carlo runs
# ATTENTION: define variable "filename1" because "filename" is already saved in the batches to be loaded; this avoids overwriting "filename"
filename1 = ["[insert the path to the output Octave files, produced by the "KORUS_r" script]/KORUS_r_" y0 "-" y1 "_batch"];

# confirm the number of batches; the default number is 20
Nofbatches = input (["Are there 20 batches with data structures? (y/n) "], "s");
if (Nofbatches == "n")
	NB = input ("enter the correct number of batches : ");
elseif (Nofbatches == "y")
	NB = 20;
else
	error ("please call the script again and re-enter the correct number of batches")
endif

# print a message about the progress in the execution of the script (optional)
printf ("Starting to extract data from file batches, assuming that each batch stores the results of 20 MC runs.\n")

# extract data, consolidate all batches, N - batch identifier
for N = 1:NB
	filebatch = [filename1 num2str(N)];
	load (filebatch)
# start loop to handle structure fields, F - field identifier
	for F = 1:20
		runname = ["run", num2str(F)];
		allname = ["run", num2str(20*(N-1)+F)];
# country identifiers: r - partner (country of origin), s - importer/home (country in focus)
		for s = [19,36]
			for r = [36,19]
# ignore a case where r = s
				if (r != s)

# ADDITIVE

					ALLdv.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdv.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdv.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdv.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);

					ALLdv.TC.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TC.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdv.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdv.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdv.TR.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TR.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TC.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TC.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TR.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TR.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TC.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TC.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TR.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TR.A.(["r",num2str(r),"_s",num2str(s)]).(runname);

					ALLdv.TC.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TC.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdv.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdv.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdv.TR.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = dv.TR.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TC.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TC.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLde.TR.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = de.TR.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TC.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TC.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLdy.TR.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = dy.TR.F.(["r",num2str(r),"_s",num2str(s)]).(runname);

# MULTIPICATIVE

					ALLrv.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLrv.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLrv.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLrv.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TC.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TDin.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TDout.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TR.AF.(["r",num2str(r),"_s",num2str(s)]).(runname);

					ALLrv.TC.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TC.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLrv.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLrv.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLrv.TR.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TR.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TC.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TC.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TR.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TR.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TC.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TC.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TDin.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TDout.A.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TR.A.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TR.A.(["r",num2str(r),"_s",num2str(s)]).(runname);

					ALLrv.TC.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TC.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLrv.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLrv.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLrv.TR.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = rv.TR.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TC.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TC.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLre.TR.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = re.TR.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TC.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TC.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TDin.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TDout.F.(["r",num2str(r),"_s",num2str(s)]).(runname);
					ALLry.TR.F.(["r",num2str(r),"_s",num2str(s)]).(allname) = ry.TR.F.(["r",num2str(r),"_s",num2str(s)]).(runname);

# end the if statement for (r != s)
				endif
# end loops for r and s
			endfor
		endfor
# end loop for the field identifier
	endfor
	clear dv* de* dy* rv* re* ry* filename B batch
# end loop for the batch identifier
endfor

# at this point, you must obtain ALL* structure arrays where the number of fields ALL**.T*.*.r**s**.*** in each structure array = number of file batched x number of Monte Carlo runs in each batch; default is 20 x 20 = 400
# each field ALL**.T*.*.r**s**.*** is a matrix or vector with the results of SDA of value added, emloyment or GDP

# print a message about the progress in the execution of the script (optional)
printf (["Now all data have been extracted and merged. " num2str(NB*F) " fields in each structure.\n"])

# define the name of the file that stores the results of the alternative, simplified estimation with the bi-proportinal allocation of market shares
filename = ["/[insert the path to the "KORUS_bp..." CSV files produced by the "KORUS_bp" script]/KORUS_bp_" y0 "-" y1 ".csv"];

# start loop for country identifiers: r - partner (country of origin), s - importer/home (country in focus)
for s = [19,36]
	for r = [36,19]
# read the estimates based on a bi-proportinal allocation of market shares, from "KORUS_bp_y0-y1.csv" files
        if (and (s==19, r==36))
            BPdv.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [2, 0, 3, 35]);
            BPdv.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [5, 0, 6, 35]);
            BPdv.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [8, 0, 9, 35]);
            BPdv.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [11, 0, 12, 35]);
            BPde.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [14, 0, 15, 35]);
            BPde.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [17, 0, 18, 35]);
            BPde.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [20, 0, 21, 35]);
            BPde.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [23, 0, 24, 35]);
            BPdy.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [26, 0, 27, 0]);
            BPdy.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [29, 0, 30, 0]);
            BPdy.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [32, 0, 33, 0]);
            BPdy.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [35, 0, 36, 0]);
            BPrv.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [38, 0, 39, 35]);
            BPrv.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [41, 0, 42, 35]);
            BPrv.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [44, 0, 45, 35]);
            BPrv.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [47, 0, 48, 35]);
            BPre.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [50, 0, 51, 35]);
            BPre.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [53, 0, 54, 35]);
            BPre.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [56, 0, 57, 35]);
            BPre.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [59, 0, 60, 35]);
            BPry.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [62, 0, 63, 0]);
            BPry.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [65, 0, 66, 0]);
            BPry.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [68, 0, 69, 0]);
            BPry.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [71, 0, 72, 0]);
            BPdv.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [74, 0, 75, 35]);
            BPdv.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [77, 0, 78, 35]);
            BPdv.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [80, 0, 81, 35]);
            BPdv.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [83, 0, 84, 35]);
            BPde.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [86, 0, 87, 35]);
            BPde.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [89, 0, 90, 35]);
            BPde.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [92, 0, 93, 35]);
            BPde.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [95, 0, 96, 35]);
            BPdy.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [98, 0, 99, 0]);
            BPdy.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [101, 0, 102, 0]);
            BPdy.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [104, 0, 105, 0]);
            BPdy.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [107, 0, 108, 0]);
            BPrv.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [110, 0, 111, 35]);
            BPrv.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [113, 0, 114, 35]);
            BPrv.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [116, 0, 117, 35]);
            BPrv.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [119, 0, 120, 35]);
            BPre.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [122, 0, 123, 35]);
            BPre.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [125, 0, 126, 35]);
            BPre.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [128, 0, 129, 35]);
            BPre.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [131, 0, 132, 35]);
            BPry.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [134, 0, 135, 0]);
            BPry.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [137, 0, 138, 0]);
            BPry.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [140, 0, 141, 0]);
            BPry.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [143, 0, 144, 0]);
            BPdv.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [146, 0, 147, 35]);
            BPdv.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [149, 0, 150, 35]);
            BPdv.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [152, 0, 153, 35]);
            BPdv.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [155, 0, 156, 35]);
            BPde.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [158, 0, 159, 35]);
            BPde.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [161, 0, 162, 35]);
            BPde.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [164, 0, 165, 35]);
            BPde.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [167, 0, 168, 35]);
            BPdy.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [170, 0, 171, 0]);
            BPdy.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [173, 0, 174, 0]);
            BPdy.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [176, 0, 177, 0]);
            BPdy.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [179, 0, 180, 0]);
            BPrv.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [182, 0, 183, 35]);
            BPrv.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [185, 0, 186, 35]);
            BPrv.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [188, 0, 189, 35]);
            BPrv.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [191, 0, 192, 35]);
            BPre.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [194, 0, 195, 35]);
            BPre.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [197, 0, 198, 35]);
            BPre.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [200, 0, 201, 35]);
            BPre.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [203, 0, 204, 35]);
            BPry.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [206, 0, 207, 0]);
            BPry.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [209, 0, 210, 0]);
            BPry.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [212, 0, 213, 0]);
            BPry.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [215, 0, 216, 0]);
        elseif(and (s==36, r==19))
            BPdv.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [218, 0, 219, 35]);
            BPdv.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [221, 0, 222, 35]);
            BPdv.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [224, 0, 225, 35]);
            BPdv.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [227, 0, 228, 35]);
            BPde.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [230, 0, 231, 35]);
            BPde.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [233, 0, 234, 35]);
            BPde.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [236, 0, 237, 35]);
            BPde.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [239, 0, 240, 35]);
            BPdy.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [242, 0, 243, 0]);
            BPdy.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [245, 0, 246, 0]);
            BPdy.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [248, 0, 249, 0]);
            BPdy.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [251, 0, 252, 0]);
            BPrv.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [254, 0, 255, 35]);
            BPrv.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [257, 0, 258, 35]);
            BPrv.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [260, 0, 261, 35]);
            BPrv.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [263, 0, 264, 35]);
            BPre.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [266, 0, 267, 35]);
            BPre.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [269, 0, 270, 35]);
            BPre.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [272, 0, 273, 35]);
            BPre.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [275, 0, 276, 35]);
            BPry.TC.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [278, 0, 279, 0]);
            BPry.TDin.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [281, 0, 282, 0]);
            BPry.TDout.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [284, 0, 285, 0]);
            BPry.TR.AF.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [287, 0, 288, 0]);
            BPdv.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [290, 0, 291, 35]);
            BPdv.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [293, 0, 294, 35]);
            BPdv.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [296, 0, 297, 35]);
            BPdv.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [299, 0, 300, 35]);
            BPde.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [302, 0, 303, 35]);
            BPde.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [305, 0, 306, 35]);
            BPde.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [308, 0, 309, 35]);
            BPde.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [311, 0, 312, 35]);
            BPdy.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [314, 0, 315, 0]);
            BPdy.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [317, 0, 318, 0]);
            BPdy.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [320, 0, 321, 0]);
            BPdy.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [323, 0, 324, 0]);
            BPrv.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [326, 0, 327, 35]);
            BPrv.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [329, 0, 330, 35]);
            BPrv.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [332, 0, 333, 35]);
            BPrv.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [335, 0, 336, 35]);
            BPre.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [338, 0, 339, 35]);
            BPre.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [341, 0, 342, 35]);
            BPre.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [344, 0, 345, 35]);
            BPre.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [347, 0, 348, 35]);
            BPry.TC.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [350, 0, 351, 0]);
            BPry.TDin.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [353, 0, 354, 0]);
            BPry.TDout.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [356, 0, 357, 0]);
            BPry.TR.A.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [359, 0, 360, 0]);
            BPdv.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [362, 0, 363, 35]);
            BPdv.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [365, 0, 366, 35]);
            BPdv.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [368, 0, 369, 35]);
            BPdv.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [371, 0, 372, 35]);
            BPde.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [374, 0, 375, 35]);
            BPde.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [377, 0, 378, 35]);
            BPde.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [380, 0, 381, 35]);
            BPde.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [383, 0, 384, 35]);
            BPdy.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [386, 0, 387, 0]);
            BPdy.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [389, 0, 390, 0]);
            BPdy.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [392, 0, 393, 0]);
            BPdy.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [395, 0, 396, 0]);
            BPrv.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [398, 0, 399, 35]);
            BPrv.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [401, 0, 402, 35]);
            BPrv.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [404, 0, 405, 35]);
            BPrv.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [407, 0, 408, 35]);
            BPre.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [410, 0, 411, 35]);
            BPre.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [413, 0, 414, 35]);
            BPre.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [416, 0, 417, 35]);
            BPre.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [419, 0, 420, 35]);
            BPry.TC.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [422, 0, 423, 0]);
            BPry.TDin.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [425, 0, 426, 0]);
            BPry.TDout.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [428, 0, 429, 0]);
            BPry.TR.F.(["r",num2str(r),"_s",num2str(s)]) = dlmread (filename, ",", [431, 0, 432, 0]);
        endif

# end loops for r and s
	endfor
endfor

# the alternative estimates are saved in structure arrays BP* where each field BP**.T*.*.r**s** contains one estimate of one variable

# clear unnecessary variables
clear F N NB NF Nofbatches allname ans filebatch filename1 filename r runname s year

# the structure arrays compiled above are available for further manipulations

# END of script
